﻿Imports System.Data.SqlClient

Public Class frmCustomerAct

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Try
            Me.Close()
            With frmCustomer
                .bindData("")
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmCustomer)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString())
        End Try
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        If (validexcust(txtCustName.Text.Trim, txtAddress.Text.Trim, lblCustomerId.Text) IsNot Nothing) Then
            MessageBox.Show("Errors: Customer Name Already Exists In This Address Please Try With Other...!")
            txtCustName.Focus()
            Exit Sub
        End If
        If (txtCustName.Text = "" Or txtPhone.Text = "" Or txtAddress.Text = "") Then
            MessageBox.Show("Errors: All field require...!")
            Exit Sub
        End If
        If CheckValid.IsPhone(txtPhone.Text.Trim) = False Then
            MessageBox.Show("Errors: Phone Number not correct format, please try the correct one...!")
            Exit Sub
        End If
        If (lblCustomerId.Text = "") Then
            Try
                Dim cmdadd As SqlCommand
                cmdadd = dbconnection.Connection.CreateCommand
                With cmdadd
                    .CommandType = CommandType.Text
                    .CommandText = "insert into Customer (CustomerName, Phone, Address, Active) values (@custname, @phone, @address, '1');"
                    .Parameters.Add("@custname", SqlDbType.NVarChar).Value = txtCustName.Text.Trim()
                    .Parameters.Add("@phone", SqlDbType.NVarChar).Value = txtPhone.Text.Trim()
                    .Parameters.Add("@address", SqlDbType.NVarChar).Value = txtAddress.Text.Trim()
                    .ExecuteNonQuery()
                End With
                MessageBox.Show("Record Added")
                Me.Close()
                With frmCustomer
                    .bindData("")
                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                    .TopLevel = False
                    .Dock = DockStyle.Fill
                    frmMain.pnBody.Controls.Add(frmCustomer)
                    .Show()
                End With
            Catch ex As Exception
                MessageBox.Show("Errors " + ex.ToString)
            End Try
        Else
            Try
                Dim cmdupdate As SqlCommand
                cmdupdate = dbconnection.Connection.CreateCommand
                With cmdupdate
                    .CommandType = CommandType.Text
                    .CommandText = "update Customer set CustomerName=@custname, Phone=@phone, Address=@address where CustomerId=@custid;"
                    .Parameters.Add("@custid", SqlDbType.NVarChar).Value = lblCustomerId.Text.Trim()
                    .Parameters.Add("@custname", SqlDbType.NVarChar).Value = txtCustName.Text.Trim()
                    .Parameters.Add("@phone", SqlDbType.NVarChar).Value = txtPhone.Text.Trim()
                    .Parameters.Add("@address", SqlDbType.NVarChar).Value = txtAddress.Text.Trim()
                    .ExecuteNonQuery()
                End With
                MessageBox.Show("Record Updated")
                Me.Close()
                With frmCustomer
                    .bindData("")
                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                    .TopLevel = False
                    .Dock = DockStyle.Fill
                    frmMain.pnBody.Controls.Add(frmCustomer)
                    .Show()
                End With
            Catch ex As Exception
                MessageBox.Show("Errors " + ex.ToString)
            End Try
        End If
    End Sub

    Private Shared Function validexcust(ByVal custname As String, ByVal address As String, ByVal custid As String)
        Try
            Dim custhave = dbconnection.ExecuteScalar("select CustomerId, CustomerName, Phone, Address from Customer" & _
            " where CustomerName=N'" + custname + "' and Address=N'" + address + "' and CustomerId<>'" + custid + "';")
            Return custhave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function
End Class